<?php
class tbdata_ctl_admin_product extends desktop_controller{
	public function index(){
		$syn_href = app::get('desktop')->router()->gen_url( array('app'=>'tbdata','ctl'=>'admin_product','act'=>'sync_detail') );
		$this->finder('tbdata_mdl_product',array(
			'title'=> $this->app->_('产品列表'),
			'actions'=>array(
				array('label' => '同步产品','confirm'=>'确定要同步这些产品吗？','submit'=>$syn_href,'target'=>'refresh'),
			),
			'use_buildin_recycle'=>true,
			'use_buildin_filter'=>true,
		 )
		);
		
	}
	public function sync_detail(){
		$this->begin();
		$queue = app::get('base')->model('queue');
		$data = array(
			'queue_title'=>app::get('tbdata')->_('同步商品'),
			'start_time'=>time(),
			'params'=>array(
				'filter'=>$_POST,
				'queue_time'=>time(),
			),
			'worker'=>'tbdata_queue_detailgets.run',
		);
		$queue->insert($data);
		$this->end(true,'已加入队列');
	}
	function debug(){
		set_time_limit(0);
		$str = <<<EOF
a:2:{s:6:"filter";a:6:{s:13:"isSelectedAll";s:5:"_ALL_";s:3:"app";s:6:"tbdata";s:3:"act";s:5:"index";s:3:"ctl";s:13:"admin_product";s:4:"view";s:1:"2";s:7:"_finder";a:2:{s:9:"finder_id";s:6:"04a1b5";s:8:"in_pager";s:1:"1";}}s:10:"queue_time";i:1314413348;}
EOF;
$params = unserialize($str);

        $model = app::get('tbdata')->model('product');
		$sync = kernel::single('tbdata_sync');
        $db = kernel::database();
		$filter = $params['filter'];
		$filter['last_modify|sthan'] = $params['queue_time'];

		echo $model->count($filter);

		$rows = $model->getList('*',$filter,0,100);
		if($rows[0]){
			foreach($rows AS $row){
				$product_id = $row['product_id'];
				$cid = $row['cid'];
				$id = $row['id'];
				$sync->get_product_detail($product_id,$cid);
			}
			return 1;
		}else{
			return 0;
		}

	}
}